package com.xxxx.yoyo.adminweb;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xxxx.yoyo.bean.Goods;
import com.xxxx.yoyo.service.IGoodsService;
import com.xxxx.yoyo.service.impl.GoodsServiceImpl;
import com.xxxx.yoyo.util.PageUtil;

/**
 * Servlet implementation class GoodsListServlet
 */
@WebServlet(name = "AdminGoodsListServlet", urlPatterns = { "/admin/goodsList" })
public class GoodsListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private IGoodsService goodsService = new GoodsServiceImpl();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		//设置导航条上的[宠物管理]处于选中状态
		request.setAttribute("flag",3);
		
		//获取前端传递过来的请求参数status,page,size
		String statusStr = request.getParameter("status");
		int status = Integer.parseInt(statusStr);
		String pageStr = request.getParameter("page");
		int page = Integer.parseInt(pageStr);
		String sizeStr = request.getParameter("size");
		int size = Integer.parseInt(sizeStr);
		
		//把status存入request中，用于设置类型的选中情况
		request.setAttribute("status",status);
		
		//分情况获取宠物列表
		if(status == 0){//获取所有宠物
			List<Goods> goodsList = goodsService.getGoodsList(page, size);
			//把goodsList存入request中，供/admin/goodsList.jsp使用
			request.setAttribute("goodsList", goodsList);
			
			//获取宠物的总数量
			long count = goodsService.getGoodsCount ();
			
			//分页信息
			String pageInfo = PageUtil.getPageTool(request,count,page,size);
			//把pageInfo存入request中，供/admin/goods1ist.jsp使用
			request.setAttribute("pageTool",pageInfo);
		}else{//获取具体榜单对应的宠物
			List<Goods> goodsList = goodsService.getGoodsListByHotType(status, page, size);
			//把goodsList存入request中，供/admin/goods1ist.jsp使用
			request.setAttribute("goodsList",goodsList);
			
			//获取指定榜单下宠物的总数量
			long count = goodsService.getGoodsCountByHotType(status);
			//分页信息
			String pageInfo = PageUtil.getPageTool(request,count,page,size);
			//把pageInfo存入request中，供/admin/goods1ist.jsp使用
			request.setAttribute("pageTool",pageInfo);
		}
		
		
		//请求转发，跳转到/admin/goods1ist.jsp
		request.getRequestDispatcher("/admin/goods_list.jsp").forward(request,response);
		
	}

}
